package com.selenium.core; 

import org.fest.swing.annotation.GUITest;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;

import com.selenium.core.functionlibrary.CustomVerification;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import com.thoughtworks.selenium.Selenium;


/**
 * Starts/Stops selenium server and launches/kills the browser
 * 
 * @author tbhadauria
 *
 */
@GUITest
public class SelTestCase extends SeleneseTestBase {

	/**
	 * Creates a new instance of <code>SeleniumServer</code>
	 */
	protected static SeleniumServer sel;

	/**
	 * Stores the reference to Selenium Object.
	 */
	protected static Selenium selenium; 

	/**
	 * Stores reference to application URL.
	 */
	protected String applicationURL;
	
	protected CustomVerification cv = new CustomVerification ();

	/**
	 * Starts selenium server before test suite execution begins.
	 * 
	 * @throws Exception
	 * 			if there was an error and selenium server could not be started.
	 */
	@BeforeSuite(alwaysRun=true)
	@Parameters("appURL")
	public void setAppURLforReportNG (String appURL) {
		//HTMLReporter.setAppURL(appURL); 
	}

	
	/**
	 * Launches test browser before execution of 
	 * first test method of Test Class.
	 * 
	 * @param selServerAdd	Address of machine where selenium server is run.
	 * @param appURL		URL of application which is test candidate.
	 * @param browserpath	Test Browser to be used for testing.
	 * 
	 */		

	@BeforeClass(alwaysRun=true) 
	@Parameters({"selServerAdd","appURL","browserPath", "port"}) 
	public void init(String selServerAdd, String appURL, String browserpath, int port) throws Exception{

		applicationURL= appURL;

		// Create default selenium object.
		selenium = new DefaultSelenium(selServerAdd, port, browserpath, appURL);		
		selenium.start();	// Launch the browser with new selenium session. 
		selenium.open(appURL);	// Open the application URL in launched browser.		
		selenium.setTimeout(getWaitPeriod());
		selenium.windowFocus();
		selenium.windowMaximize();
	}

	/**
	 * Closes the browser after last test method of Test Class
	 * 
	 */
	@AfterClass(alwaysRun=true)
	public void stop() {
		selenium.stop();	// Kills the browser
	}

	
	/**
	 * Returns the wait period required to load web page.
	 * 
	 * @return
	 * 		Returns the wait period required to load web page.
	 */
	public static String getWaitPeriod() {		
		return("600000");	 			
	}
	
	/**
	 * 
	 * Combines click and wait Operations
	 * 
	 * @param objectIdentifier Represents web page object
	 */
	public void clickAndWait(String objectIdentifier) {
		
		selenium.click(objectIdentifier);
		selenium.waitForPageToLoad(getWaitPeriod());		
	}
}
